package com.tcm.store.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.base.core.dozer.DozerConvert;
import com.base.core.exception.ServiceException;
import com.base.core.pager.PageBean;
import com.base.core.pager.PageContext;
import com.base.core.utils.ListUtils;
import com.base.core.utils.R;
import com.base.genid.generator.UidGenerator;
import com.tcm.core.service.ConfigService;
import com.tcm.store.domain.dto.StoreDTO;
import com.tcm.store.domain.entity.Store;
import com.tcm.store.domain.enums.BusinessCodeEnum;
import com.tcm.store.mapper.StoreDosageMapper;
import com.tcm.store.mapper.StoreMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService(version = "0.0.1-alpha", retries = 3, timeout = 3000)
public class StoreServiceImpl implements StoreService {

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private StoreDosageMapper storeDosageMapper;

    @DubboReference(version = "0.0.1-beta", group = "cached-uid", retries = 3, check = false, timeout = 3000)
    private UidGenerator uidGenerator;

    @DubboReference(version = "0.0.1-alpha", retries = 3, check = false, timeout = 3000)
    private ConfigService configService;

    @Override
    public Long save(StoreDTO storeDto) throws ServiceException{
        if (ObjectUtil.isEmpty(storeDto)) {
            R.throwFail(BusinessCodeEnum.STORE_OBJECT_NOT_EXISTS);
        }

        Integer idsCount = storeMapper.checkStoreExistsByName(storeDto.getName(), null);
        if (idsCount.intValue() > 0) {
            R.throwFail(BusinessCodeEnum.STORE_NAME_NOT_DUPLICATE);
        }
        Store store = DozerConvert.copyProperties(storeDto, Store.class);
        store.setId(uidGenerator.getUID());
        store.setCreateTime(DateUtil.date());
        store.setUpdateTime(DateUtil.date());
        store.setDeleted(new Byte("0"));
        storeMapper.insert(store);
        return store.getId();
    }

    @Override
    public StoreDTO findStoreById(Long id) throws ServiceException{
        Store store = storeMapper.findStoreById(id);
        StoreDTO storeDTO = null;
        if (ObjectUtil.isNotEmpty(store)) {
            storeDTO = DozerConvert.copyProperties(store, StoreDTO.class);
        }
        return storeDTO;
    }

    @Override
    public void update(StoreDTO storeDto) throws ServiceException {
        if (ObjectUtil.isEmpty(storeDto.getId())) {
            R.throwFail(BusinessCodeEnum.STORE_ID_NOT_EXISTS);
        }
        Integer count = storeMapper.checkStoreExistsByName(storeDto.getName(), storeDto.getId());
        if (count.intValue() > 0) {
            R.throwFail(BusinessCodeEnum.STORE_NAME_NOT_DUPLICATE);
        }
        Store store = DozerConvert.copyProperties(storeDto, Store.class);
        store.setUpdateTime(DateUtil.date());
        storeMapper.update(store);
    }

    /**
     * 根据店铺名称分页查询店铺列表
     *
     * @param start     当前页码
     * @param pagesize  每页记录数
     * @return
     */
    @Override
    public PageBean<StoreDTO> listStorePage(Long start, Long pagesize) throws Exception {
        Long count = storeMapper.countStore();
        PageContext.setCurrentPage(start);
        PageContext.setPageSize(pagesize);
        PageContext.setDataCount(count);
        if(count.equals(0)){
            return new PageBean<>(null);
        }

        List<Store> stores = storeMapper.listStorePage(PageContext.getStart(), pagesize);
        List<StoreDTO> storeDTOS = DozerConvert.copyList(stores, StoreDTO.class);
        PageContext.clear();
        return new PageBean<>(storeDTOS);
    }

    /**
     * 分页查询供应商列表
     *
     * @param start    当前页码
     * @param pagesize 每页记录数
     * @return
     */
    @Override
    public PageBean<StoreDTO> listStorePageByDosageId(Long dosageId, Long start, Long pagesize) throws Exception {
        List<Long> storeIds = storeDosageMapper.listStoreIdsByDosageId(dosageId);
        if(ListUtils.isEmpty(storeIds)){
            return new PageBean<>(null, 0L, start,pagesize);
        }
        Long count = storeMapper.countStoreByIds(storeIds);
        PageContext.setCurrentPage(Long.valueOf(start));
        PageContext.setPageSize(Long.valueOf(pagesize));
        PageContext.setDataCount(count);
        if(count.intValue() == 0){
            return new PageBean<>(null);
        }

        List<Store> supplierVoList = storeMapper.listStorePageByIds(storeIds,PageContext.getStart(),
                PageContext.getPageSize());
        List<StoreDTO> storeDTOList = DozerConvert.copyList(supplierVoList, StoreDTO.class);
        PageContext.clear();
        return new PageBean<>(storeDTOList);
    }

    /**
     * 逻辑删除
     *
     * @param id
     * @return
     */
    @Override
    public int deleteLogicById(Long id) {
        return storeMapper.deleteLogicById(id);
    }

    /**
     * 永久删除
     *
     * @param id
     * @return
     */
    @Override
    public int deleteById(Long id) {
        return storeMapper.deleteById(id);
    }

    @Override
    public String logisticsFee(Long provinceId, Long storeId) {
        //获取基础运费和跨省运费
        String fare_base = configService.findValue("fare_base", "14");
        String fare_interprovincial = configService.findValue("fare_interprovincial", "18");

        Store store = storeMapper.findStoreById(storeId);

        if(ObjectUtil.isNull(store)){
            R.throwFail(BusinessCodeEnum.STORE_NOT_EXISTS);
        }

        if (store.getProvinceId().equals(provinceId)){
            return fare_base;
        }

        return fare_interprovincial;
    }


}
